﻿using System;
using System.IO;
using System.Security.AccessControl;

namespace Fly.Common.Helper
{
    /// <summary>
    /// 文件操作辅助类
    /// </summary>
    public static class FileHelper
    {
        #region 递归创建文件夹
        /// <summary>  
        /// 递归创建文件夹  
        /// </summary>  
        /// <param name="directoryName">文件夹的路径</param>  
        public static void CreateDirectory(string directoryName)
        {
            string sParentDirectory = Path.GetDirectoryName(directoryName);
            if (!string.IsNullOrWhiteSpace(sParentDirectory))
            {
                if (!Directory.Exists(sParentDirectory))
                {
                    CreateDirectory(sParentDirectory);
                }
            }
            if (!Directory.Exists(directoryName))
            {
                Directory.CreateDirectory(directoryName);
            }
        } 
        #endregion

        #region 递归创建目录并设置目录安全性
        /// <summary>
        /// 递归创建目录并设置目录安全性
        /// </summary>
        /// <param name="path">目录路径</param>
        /// <param name="directorySecurity">目录的安全机制</param>
        /// <returns></returns>
        public static bool CreateFolder(string path, DirectorySecurity directorySecurity)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                return false;
            }
            try
            {
                if (Directory.Exists(path) && directorySecurity == null)
                {
                    return true;
                }
                DirectoryInfo directory = new DirectoryInfo(path);
                if (Directory.Exists(path))
                {
                    directory.SetAccessControl(directorySecurity);
                    return true;
                }
                string[] paths = path.Split('\\');
                if (paths.Length > 1)
                {
                    string tempPath = paths[0];
                    for (int i = 1; i < paths.Length; i++)
                    {
                        tempPath += "\\" + paths[i];
                        if (!Directory.Exists(tempPath))
                        {
                            Directory.CreateDirectory(tempPath);
                        }
                    }
                }
                return true;
            }
            catch (Exception)
            {

                return false;
            }
        } 
        #endregion

        #region 尝试删除文件
        /// <summary>
        /// 尝试删除文件
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool TryDelete(string filePath)
        {
            try
            {
                File.Delete(filePath);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        } 
        #endregion

        #region 将指定目录复制到目标位置
        /// <summary>
        /// 将指定目录复制到目标位置
        /// </summary>
        /// <param name="sourcePath">源目录</param>
        /// <param name="destinationPath">目标目录</param>
        /// <param name="overwriteexisting">是否覆盖已存在文件</param>
        /// <returns></returns>
        public static bool CopyDirectory(string sourcePath, string destinationPath, bool overwriteexisting)
        {
            bool flag;
            try
            {
                sourcePath = (sourcePath.EndsWith("\\") ? sourcePath : string.Concat(sourcePath, "\\"));
                destinationPath = (destinationPath.EndsWith("\\") ? destinationPath : string.Concat(destinationPath, "\\"));
                if (Directory.Exists(sourcePath))
                {
                    if (!Directory.Exists(destinationPath))
                    {
                        Directory.CreateDirectory(destinationPath);
                    }
                    string[] files = Directory.GetFiles(sourcePath);
                    foreach (string t in files)
                    {
                        FileInfo fileInfo = new FileInfo(t);
                        fileInfo.CopyTo(string.Concat(destinationPath, fileInfo.Name), overwriteexisting);
                    }
                    string[] directories = Directory.GetDirectories(sourcePath);
                    foreach (string str in directories)
                    {
                        CopyDirectory(str, string.Concat(destinationPath, (new DirectoryInfo(str)).Name),overwriteexisting);
                    }
                }
                flag = true;
            }
            catch (Exception)
            {
                flag = false;
            }
            return flag;
        } 
        #endregion

        #region 数据流转换
        /// <summary> 
        /// 将 Stream 转成 byte[] 
        /// </summary> 
        public static byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);

            // 设置当前流的位置为流的开始 
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }
        /// <summary> 
        /// 将 byte[] 转成 Stream 
        /// </summary> 
        public static Stream BytesToStream(byte[] bytes)
        {
            return new MemoryStream(bytes);
        }   
        #endregion

        #region 将指定路径的文件读入字符串
        /// <summary>
        /// 将指定路径的文件读入字符串
        /// </summary>
        /// <param name="aPath"></param>
        /// <returns></returns>
        public static string ReadTextFile(string aPath)
        {
            if (string.IsNullOrWhiteSpace(aPath))
            {
                return string.Empty;
            }
            string text = string.Empty;
            if (File.Exists(aPath))
            {
                using (TextReader tr = new StreamReader(aPath))
                {
                    text = tr.ReadToEnd();
                }
            }
            return text;
        } 
        #endregion
    }
}
